//index.js
//获取应用实例
var app = getApp()
var url = app.globalData.path + "/weixin/coffee/xcx/coffeePass/list";
var currentPage = 1;
var size = 10;
var type = "";
var currentLocation = "";
var pullBol = 0;
var areaId="";
var isMember;
var fromType="";
var fromId="";
//获取特权列表
var GetList = function (that) {
  that.setData({
    loadHidden: false
  });
  pullBol = 1;
  wx.request({
    url: url,
    data: {
      currentPage: currentPage,
      size: size,
      type: type,
      currentLocation: currentLocation,
      areaId: areaId,
      userId: that.data.userInfo.userId
    },
    success: function (res) {
      var list = that.data.list;
      var areaName = "北京";
      for (var i = 0; i < res.data.list.length; i++) {
        list.push(res.data.list[i]);
      }
      if (res.data.areaId == "2") {
        areaName = "上海";
        that.setData({
          areaImgUrl: "http://img.icon.wishtv.com.cn/shanghai.jpg",
          headerN: "headerN"
        })
      } else {
        that.setData({
          areaImgUrl: "http://img.icon.wishtv.com.cn/beijing.jpg",
          headerN: ""
        })
      }
      that.setData({
        list: list,
        areaName: areaName
      });
      currentPage++;
      that.setData({
        loadHidden: true
      });
      pullBol = 0;
    }
  });
}

//获取特权列表
var GetListPage1 = function (that) {
  currentPage=1;
  that.setData({
    loadHidden: false,
    list: []
  });
  pullBol = 1;
  wx.request({
    url: url,
    data: {
      currentPage: 1,
      size: size,
      type: type,
      currentLocation: currentLocation,
      areaId: areaId,
      userId: that.data.userInfo.userId
    },
    success: function (res) {
      var list = that.data.list;
      var areaName = "北京";
      for (var i = 0; i < res.data.list.length; i++) {
        list.push(res.data.list[i]);
      }
      if (res.data.areaId == "2") {
        areaName = "上海";
        that.setData({
          areaImgUrl: "http://img.icon.wishtv.com.cn/shanghai.jpg",
          headerN: "headerN"
        })
      } else {
        that.setData({
          areaImgUrl: "http://img.icon.wishtv.com.cn/beijing.jpg",
          headerN: ""
        })
      }
      that.setData({
        list: list,
        areaName: areaName
      });
      currentPage++;
      that.setData({
        loadHidden: true
      });
      pullBol = 0;
    }
  });
}

//统计数据来源
var report = function (that) {
  wx.request({
    url: app.globalData.path + '/weixin/coffee/xcx/user/report',
    data: {
      fromType: fromType,
      fromId: fromId,
      userId: that.data.userInfo.userId
    },
    success: function (res) {
         console.info(res);
    }
  });
}

//获取用户信息
var getWishUser = function (that) {
  wx.request({
    url: app.globalData.path + '/weixin/coffee/xcx/user/getUserInfo',
    data: {
      userId: that.data.userInfo.userId
    },
    method: 'GET',
    success: function (res) {
      if (res.data.isMember == true && res.data.phone == "") {
        that.setData({
          bangdingHidden: false
        })
      }
      that.setData({
        wishUser: res.data
      })
    }
  })
}

Page({
  data: {
    wishUser: {},
    userInfo:{},
    loadHidden:true,
    jihuoHidden:true,
    scrollHidden: true,
    errorHidden:true,
    areaImgUrl: "http://img.icon.wishtv.com.cn/beijing.jpg",
    headerN: "",
    areaName: "",
    scrollHeight:"",
    list: []
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (options) {
    if (options.currentPage){
      currentPage = options.currentPage
    }
    if (options.fromType) {
      fromType = options.fromType
    }
    if (options.fromId) {
      fromId = options.fromId
    }

    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
      getWishUser(that);
      //统计
      report(that);
    })
    //调用定位
    app.getLocation(function(data){
      currentLocation = app.globalData.longitude + "," + app.globalData.latitude
      console.info("currentLocation" + currentLocation)
          wx.request({
            url: app.globalData.path + '/wish/saveLocation',
            data: {
              latitude: app.globalData.latitude,
              longitude: app.globalData.longitude,
              areaId: areaId
            },
            method: 'GET',
            success: function (res) {
              areaId = res.data.areaId;
              GetListPage1(that);
            }
          })    
    })
    //给scroll-view设置默认高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    })
  },
  onShow: function () {
    //   在页面展示之后先获取一次数据
    var that = this;
    GetList(that);
  },
  bindDownLoad: function () {
    //   该方法绑定了页面滑动到底部的事件
    var that = this;
    if (pullBol == 0) {
      GetList(that);
    }
  },
  //切换城市
  checkoutCity: function () {
    var that = this
    wx.showActionSheet({
      itemList: ['北京', '上海'],
      success: function (res) {
        if (!res.cancel) {
          currentPage = 1
          that.setData({
            list: []
          })
          if (res.tapIndex === 0) {
            areaId = "1"
            GetList(that)
          } else {
            areaId = "2"
            GetList(that)
          }
        }
      }
    })
  },
  //去个人中心
  toCenter: function () {
    wx.navigateTo({
      url: '../center/center?currentLocation=' + currentLocation
    })
  },
  //去开通
  toPromoCode: function (e) {
    var cardtype = e.currentTarget.dataset.cardtype;
    wx.navigateTo({
      url: '../payOrder/payOrder?cardType=' + cardtype
    })
  },

  jihuo: function () {
    this.setData({
      jihuoHidden: false
    })
  },
  //去文章详情
  toDetail:function(e){
    var restaurantId = e.currentTarget.dataset.restaurantid;
    var articleId = e.currentTarget.dataset.articleid;
    isMember = this.data.wishUser.isMember;
    wx.navigateTo({
      url: '../detail/detail?restaurantId=' + restaurantId + "&articleId=" + articleId + "&isMember=" + isMember
    })
  },
  //分享
  onShareAppMessage: function () {
    return {
      title: '咖啡一卡通',
      desc: '精选咖啡厅供您随意挑选',
      path: '/pages/index/index'
    }
  },
  //地图导航
  openMap: function (e) {
    console.info(e)
    var lat = e.currentTarget.dataset.lat;
    var lng = e.currentTarget.dataset.lng;
    var restaurantName = e.currentTarget.dataset.restaurantname;
    var restaurantAddress = e.currentTarget.dataset.restaurantaddress;
    wx.openLocation({
      latitude: lat,
      longitude: lng,
      scale: 28,
      name: restaurantName,
      address: restaurantAddress
    })
  },
  //去享用
  toEnjoy: function (e) {
    var restaurantId = e.currentTarget.dataset.restaurantid;
    isMember = this.data.wishUser.isMember;
    if (isMember == true) {
      wx.navigateTo({
        url: '../enjoy/enjoy?restaurantId=' + restaurantId
      })
    } else {
      this.setData({
        errorHidden: false,
        errorText: "您不是WISH VIP，快开通会员来享用吧！"
      })
    }

  },
  cancel: function () {
        this.setData({
      jihuoHidden: true
        });
    },
  closeErrorDialog: function () {
    this.setData({
      errorHidden: true
    })
  },
  submit:function(){
    wx.navigateTo({
      url: '../applyCard/applyCard',
    })
  }
})
